using System.Collections;
using BasicScript;
using Game.Main.Event;
using Game.Manager;
using UnityEngine;

namespace Game.Core.Loader
{
	public class SpriteLoader : EventObject
	{
		public string path;

		public string resourceName;

		public Sprite[] resource;

		public AssetBundle assetBundle;

		public object parameters;

		private bool isUnloadAB;

		private bool isCommonRes;

		public SpriteLoader(string path, string resourceName, bool isUnloadAB = true, bool isCommonRes = false)
		{
			this.path = path;
			this.resourceName = resourceName;
			this.isUnloadAB = isUnloadAB;
			this.isCommonRes = isCommonRes;
		}

		public void Load()
		{
			if (path != null)
			{
				Client.Instance.StartCoroutine(StartLoadResource());
			}
			else
			{
				Fire(new BaseEvent(LoadEvent.LOAD_ERROR));
			}
		}

		public Sprite GetSprite(string name)
		{
			for (int i = 0; i < resource.Length; i++)
			{
				if (resource[i].name.Equals(name))
				{
					return resource[i];
				}
			}
			return null;
		}

		private IEnumerator StartLoadResource()
		{
			AssetBundleManager abManager = Singleton<AssetBundleManager>.Instance;
			string url = ClientConfig.GetPath(path);
			AssetBundleData abData;
			if (!isCommonRes)
			{
				if (!abManager.loadedResources.ContainsKey(url))
				{
					abManager.loadedResources.Add(url, new AssetBundleData(AssetBundle.LoadFromFileAsync(url)));
					abData = abManager.loadedResources[url];
					yield return abData.www;
				}
				else
				{
					abData = abManager.loadedResources[url];
					abData.quoteNum++;
					while (!abData.www.isDone)
					{
						yield return new WaitForSeconds(0.05f);
					}
				}
			}
			else
			{
				if (abManager.loadedResources.ContainsKey(url))
				{
					abData = abManager.loadedResources[url];
					abManager.loadedResources.Remove(url);
				}
				else
				{
					abData = new AssetBundleData(AssetBundle.LoadFromFileAsync(url));
				}
				yield return abData.www;
			}
			if (abData.www.assetBundle != null)
			{
				if (abData.assetBundle == null)
				{
					abData.assetBundle = abData.www.assetBundle;
				}
				if (abData.sprites == null)
				{
					abData.sprites = abData.assetBundle.LoadAllAssets<Sprite>();
				}
				resource = abData.sprites;
				if (isUnloadAB)
				{
					if (ClientConfig.PLATFORM != 0)
					{
						abData.quoteNum--;
						if (abData.quoteNum <= 0)
						{
							abData.Dispose();
							abManager.loadedResources.Remove(url);
						}
					}
				}
				else
				{
					assetBundle = abData.assetBundle;
				}
				Fire(new BaseEvent(LoadEvent.LOAD_COMPLETE));
			}
			else
			{
				Trace.Log("Sprite资源加载失败：" + url);
				Fire(new BaseEvent(LoadEvent.LOAD_ERROR));
			}
		}

		public void Destroy(bool isDestroyAssetBundle = false)
		{
			base.Destroy();
			if ((isDestroyAssetBundle || isUnloadAB) && assetBundle != null && ClientConfig.PLATFORM != 0)
			{
				AssetBundleManager instance = Singleton<AssetBundleManager>.Instance;
				AssetBundleData assetBundleData = null;
				string key = ClientConfig.GetPath(path);
				if (instance.loadedResources.ContainsKey(key))
				{
					assetBundleData = instance.loadedResources[key];
				}
				if (assetBundleData != null)
				{
					assetBundleData.quoteNum--;
					if (assetBundleData.quoteNum <= 0)
					{
						assetBundleData.Dispose();
						instance.loadedResources.Remove(key);
					}
				}
				assetBundle = null;
			}
			resource = null;
		}
	}
}
